package com.hnguigu.health.houduan.lcj.controller_lcj;

import com.google.gson.Gson;
import com.hnguigu.health.houduan.lcj.pojo_lcj.PagVo_lcj;
import com.hnguigu.health.houduan.lcj.pojo_lcj.TOrder_lcj;
import com.hnguigu.health.houduan.lcj.pojo_lcj.TSetmealCheckgroup_lcj;
import com.hnguigu.health.houduan.lcj.pojo_lcj.TSetmeal_lcj;
import com.hnguigu.health.houduan.lcj.service_lcj.TOrderService_lcj;
import com.hnguigu.health.houduan.lcj.service_lcj.TSetmealCheckgroupService_lcj;
import com.hnguigu.health.houduan.lcj.service_lcj.TSetmealService_lcj;
import com.hnguigu.health.pojo.Img;
import com.hnguigu.health.pojo.TSportsItemLibrary;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import java.io.File;
import java.io.IOException;
import java.util.List;
import java.util.Map;
import java.util.UUID;

@Controller
@RequestMapping("/tsetmeal")
public class TSetmealController_lcj {

    @Autowired(required = false)
    TSetmealService_lcj tset;

    @Autowired(required = false)
    TOrderService_lcj ts;

    @Autowired(required = false)
    TSetmealCheckgroupService_lcj tsmc;

    @PostMapping("/findAll")
    @ResponseBody
    public PagVo_lcj<TSetmeal_lcj> findAll(@RequestBody Map<String,String> map){
        return tset.findAll1(Integer.valueOf(map.get("page1")),Integer.valueOf(map.get("num1")),map.get("name"));
    }
    @PostMapping("/getById")
    @ResponseBody
    public PagVo_lcj<TSetmeal_lcj> getById(@RequestBody Map<String,String> map){
        return tset.findAll2(Integer.valueOf(map.get("page2")),Integer.valueOf(map.get("num2")),map.get("mid"));
    }
    @GetMapping("/delId")
    @ResponseBody
    public Integer delId(String id){
        List<TOrder_lcj> bySetId = ts.getBySetId(id);
        if (bySetId.size()==0){
            tset.delById(id);
            return 1;
        }else{
            return 2;
        }
    }
    @GetMapping("/byId")
    @ResponseBody
    public Integer byId(String code){
        TSetmeal_lcj byid = tset.ById(code);
        if(byid==null){
            return 1;
        }else{
            return 2;
        }
    }
    @PostMapping("/addse")
    @ResponseBody
    public Integer addse(@RequestParam("files") MultipartFile[] files, @RequestParam("files1") String tsetmeal, @RequestParam("files2") String checkId) throws IOException {


        Gson gson = new Gson();
        TSetmeal_lcj o = gson.fromJson(tsetmeal,TSetmeal_lcj.class);
        System.out.println("----------------------------------------------------");
        System.out.println(files);
        System.out.println(tsetmeal);
        System.out.println(checkId);

        for (MultipartFile file : files) {
            //随机生成新的文件名称
            String uuid = UUID.randomUUID().toString();
            //获取原来文件名称
            String originalFilename = file.getOriginalFilename();
            //获取文件名称的后缀
            String substring = originalFilename.substring(originalFilename.lastIndexOf("."));
            //组装新的文件名称带有后缀
           String newFileName = uuid+substring;
            //上传的文件存储的路径地址
            File file1 = new File("D:\\s3\\health-managers\\src\\main\\webapp\\img\\" + newFileName);
            //开始上传
            file.transferTo(file1);

            o.setImg(newFileName);


        }


        TSetmeal_lcj lastbean = tset.Lastbean();
        String code = lastbean.getCode();
        String newTmid="";
        String substring="";
        if(lastbean==null){
            newTmid="TC001";
        }else{
            substring = code.substring(2, code.length());
        }
        if(Integer.parseInt(substring)<10){
            newTmid= "TC00"+(Integer.parseInt(substring)+1);
        }
        if(Integer.parseInt(substring)>=10){
            newTmid= "TC0"+(Integer.parseInt(substring)+1);
        }
        if(Integer.parseInt(substring)>=100){
            newTmid= "TC"+(Integer.parseInt(substring)+1);
        }
        o.setCode(newTmid);

        System.out.println(o+"套餐添加");
        tset.addSet(o);
        TSetmeal_lcj tSetmeal_lcj = tset.ById(o.getName());
        Integer id = tSetmeal_lcj.getId();
        String createtime = o.getCreatetime();
        String[] split = checkId.split(",");
        TSetmealCheckgroup_lcj tSetmealCheckgroup_lcj = new TSetmealCheckgroup_lcj();
        for (int i=0;i<split.length;i++){
            tSetmealCheckgroup_lcj.setSetmealId(id);
            tSetmealCheckgroup_lcj.setCheckgroupId(Integer.valueOf(split[i]));
            tSetmealCheckgroup_lcj.setCreatetime(createtime);
            tsmc.addtsetCheck(tSetmealCheckgroup_lcj);
        }
        return 1;
    }
    @GetMapping("/getBytsetid")
    @ResponseBody
    public List<TSetmealCheckgroup_lcj> getBytsetid(String tsetid){
        return tsmc.getBySetID(tsetid);
    }
}
